<?php
require_once APP . '/application/admincontrollers/AdminAction.php';
require_once(APP . '/application/models/LinkModel.php');

/**
 * 后台管理主页
 * @package admin
 */
class LinkController extends AdminAction
{
    
	/**
	 * Enter description here...
	 *
	 * @var LinkModel
	 */
	private $link;
    public function init()
    {    	
        $this->link = new LinkModel($this->db);
    }


    /**
     * 加载框架页
     */
    public function addAction()
    {
    }
    
    /**
     * 发布新闻
     *
     */
    public function doaddAction()
    {
    	$type = $this->getParam("type");
    	$param = $this->getAllParams();
    	if ($this->getParam("top") == 1){
    		$param['seq'] = $this->db->fetchOne("select max(seq)+1 from link");
    	}
    	
    	$this->link->add($param);
    	
    	$this->view->msg = '添加成功';
    		
    	$this->view->url = $this->url($this->view->baseUrl, "link", "add", "type=" . $type, "admin");
    	$this->showmessage();
    }
    
    /**
     * 文章管理
     *
     */
    public function listAction()
    {
    	$type = $this->getParam("type");
    	$nowp = $this->getParam("nowp");
    	$per_page = 20;
    	
		$result = $this->link->fetchAll($nowp, $per_page);
    	
    	$this->view->nowp = $nowp;
        $this->view->list = $result['list'];
        $this->view->page = $result['page'];
        $this->view->type = $type;
    }
    
    public function delAction()
    {
    	$id = (int) $this->getParam("id");
    	$nowp = (int) $this->getParam("nowp");
    	$type = $this->getParam("type");
    	
    	$this->link->del($id);
    	
    	header("location:" . $this->url($this->view->baseUrl, "link", "list", "nowp=" . $nowp . '&type='. $type, "admin"));
    }
    
    public function editAction()
    {
    	$id = (int) $this->getParam("id");
    	$nowp = (int) $this->getParam("nowp");
    	$type = $this->getParam("type");
    	$content = $this->link->show($id);
    	
        $this->view->nowp = $nowp;
        $this->view->content = $content;
        $this->view->type = $type;
    }
    
    public function doeditAction()
    {
    	$parms = $this->getAllParams();
    	
    	$nowp = $this->getParam("nowp");
    	$id = $this->getParam("id");
    	
    	if ($this->getParam("top") == 1){
    		$parms['seq'] = $this->db->fetchOne("select max(seq)+1 from link");
    	}
    	$this->link->change($id, $parms);
    	
    	$this->view->msg = '编辑成功';
    	$this->view->url = $this->url($this->view->baseUrl, "link", "list", "nowp=" . $nowp . '&type=' . $this->getParam("type"), "admin");
    	$this->showmessage();
    }
    
    public function setAction()
    {
    	$url_param = $this->getParam("url", null);
    	if (!empty($url_param))
    		FileCache::getInstance()->set("meet_reg_url", $url_param);
    	
    	$url = FileCache::getInstance()->get("meet_reg_url");
    	
    	if (empty($url))
    		$url = "http://";
    	
    	
    	$this->view->url = $url;
    }
    
    public function imageAction()
    {
    	$index_image = FileCache::getInstance()->get("index_image");
    	if ($index_image == null)
    		$index_image = array();
    	
    	if ($this->getParam("button") != null){
    		$index_image['display'] = !! $this->getParam("display");
    		$index_image['url'] = $this->getParam("url");
    		$image = Util::upfile("image", "image");
    		!empty($image) && $index_image['image'] = $image;
    		
    		FileCache::getInstance()->set("index_image", $index_image);
    		
    		$this->view->msg = '编辑成功';
    		$this->view->url = $this->url($this->view->baseUrl, "link", "image", "", "admin");
    		$this->showmessage();
    	}
    	
    	$this->view->content = $index_image;
    }
}